/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.member')
        .controller('ParentInfoCtrl', ParentInfoCtrl);

    function ParentInfoCtrl(studentId,$scope,AppUtils,MemberModel,$ionicPopup,$window,store,Global) {
        $scope.$on('$ionicView.beforeEnter', function (event, viewData) {
            viewData.enableBack = true;
        });

        $scope.studentId = studentId;

        var vm = this;
        vm.memberInfoNotChange = true;
        vm.memberEdit = false;
        vm.disableLeavenMsgBtn = false;
        vm.memberInfo = {};

        $scope.change = function(){
            vm.memberInfoIsChange = false;
        }

        vm.loginAccount = store.get("loginAccount");
        vm.showLeaveMsgBtn = false;
        if(!AppUtils.isUndefinedOrNull(vm.loginAccount)){
            if(vm.loginAccount.memberType == '1'){
                vm.showLeaveMsgBtn = true;
            }
        }



        $scope.goBack = function(){
            if(vm.memberEdit == true){
                var confirmPopup = $ionicPopup.confirm({
                    title: "提示",
                    template: "是否退出家长信息修改？",
                    buttons: [
                        {
                            text: '是',
                            onTap: function() {
                                vm.memberEdit = false;
                            }
                        },
                        {
                            text: '<b>否</b>',
                            type: 'button-positive',
                            onTap: function() {
                                vm.memberEdit = true;
                            }
                        }
                    ]
                });
            }else{
                $window.history.go(-1);
            }
        }


        $scope.editMemberInfo = function(){
            if(vm.memberEdit == true){
                var confirmPopup = $ionicPopup.confirm({
                    title: "提示",
                    template: "是否退出家长信息修改？",
                    buttons: [
                        {
                            text: '是',
                            onTap: function() {
                                vm.memberEdit = false;
                            }
                        },
                        {
                            text: '<b>否</b>',
                            type: 'button-positive',
                            onTap: function() {
                                vm.memberEdit = true;
                            }
                        }
                    ]
                });
            }else{
                vm.memberEdit = true;
            }
        }

        //parentId = "A83FA905F6044E9CA30C52FDCD351794";
        var memberType = "2";

        $scope.studentInfo = {};
        $scope.fatherInfo = {};
        $scope.motherInfo = {};

        vm.init = function(){
            if(!AppUtils.isUndefinedOrNull(studentId)){
                MemberModel.getParentInfo({id: studentId}).$promise.then(function(response){
                    if(response.stat == 'err'){
                        AppUtils.showAlert(response.msg);
                    }else{
                        if(AppUtils.isUndefinedOrNull(response.memberList)){
                            AppUtils.showAlert("未找到此家长信息");
                            AppUtils.goBack();
                        }else{
                            angular.forEach(response.memberList,function(obj){
                                if(obj.memberType == '3'){
                                    $scope.studentInfo = obj;
                                    if(AppUtils.isUndefinedOrNull(obj.avatarPath)){
                                        $scope.studentInfo.avatarPath = Global.default_avatarPath;
                                    }
                                }else if(obj.memberType == '2'){
                                    if(obj.memberRelType == 'father'){
                                        $scope.fatherInfo = obj;
                                    }else if(obj.memberRelType == 'mother'){
                                        $scope.motherInfo = obj;

                                    }
                                }
                            })
                        }
                    }
                })
            }else{
                AppUtils.showAlert("未找到家长信息！");
            }
        }
        vm.init();
        $scope.submit = function() {
            if(AppUtils.isUndefinedOrNull(vm.memberInfo.memberName)){
                AppUtils.showAlert("请输入家长姓名！");
                return false;
            }
            if(AppUtils.isUndefinedOrNull(vm.memberInfo.memberPhone)){
                AppUtils.showAlert("请输入家长手机号码！");
                return false;
            }
            if(AppUtils.isUndefinedOrNull(vm.memberInfo.memberAddress)){
                AppUtils.showAlert("请输入家长居住地址！");
                return false;
            }

            MemberModel.saveParentMemberInfo(vm.memberInfo).$promise.then(function(response){
                var result = response.result;
                if(!AppUtils.isUndefinedOrNull(result) && result == 'success'){
                    AppUtils.showAlert("保存家长信息成功！");
                    vm.memberEdit = false;
                }else{
                    AppUtils.showAlert("保存失败！");
                }

            })
        };
    }
})();
